package org.fuys.coder.application;

import org.fuys.coder.common.entity.Result;
import org.fuys.coder.domain.config.service.impl.CoderActivityConfig;
import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.common.holder.UserHolder;
import org.fuys.coder.domain.act.lottery.model.req.DrawLotteryReq;
import org.fuys.coder.domain.act.lottery.model.req.draw.DrawReq;
import org.fuys.coder.domain.act.lottery.model.res.draw.DrawResult;
import org.fuys.coder.domain.act.lottery.service.activity.ILotteryActivityService;
import org.fuys.coder.domain.act.lottery.service.draw.IDrawService;
import org.fuys.coder.domain.act.support.model.req.ActRedemptionReq;
import org.fuys.coder.domain.act.support.model.vo.ActRedemptionVO;
import org.fuys.coder.domain.redemption.model.vo.RedemptionResult;
import org.fuys.coder.domain.redemption.service.IRedemptionService;
import org.fuys.coder.domain.signup.service.ISignUpService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application
 * @className: ActivityController
 * @author: WangJing
 * @description: 活动控制器 如签到 抽奖等
 * @date: 2024/6/30 15:37
 * @version: 1.0
 */
@RestController
@RequestMapping("/fuyscoder/activity")
public class ActivityController {

    @Resource
    private ISignUpService signUpService;

    @Resource
    private IDrawService drawService;

    @Resource
    private ILotteryActivityService lotteryActivityService;

    @Resource
    private IRedemptionService redemptionService;

    @Resource
    private CoderActivityConfig activityConfig;


    @GetMapping("/signup")
    public Result signUp(){
        final Integer integer = UserHolder.get();
        if(integer==null){
            throw new BusinessException("用户的信息有误!");
        }
        String msg= ResultMessageConstants.SIGN_UP_SUCCESS;
        if (!signUpService.userSignUp(integer,activityConfig.getSignupPoint())) {
            msg=ResultMessageConstants.SIGN_UP_FAILED;
        }
        return Result.ok().msg(msg);
    }

    @PostMapping("/draw/lottery")
    @Transactional
    public Result drawLottery(@RequestBody DrawLotteryReq req){
        //首先判断用户登录状态
        final Integer userId = UserHolder.get();
        if(userId==null){
            return Result.error(ResultMessageConstants.NO_USER_TOKEN);
        }
        try {
            //检测活动参与次数
            if(!lotteryActivityService.checkTakeCount(userId,req.getActId())){
                return Result.ok().msg(ResultMessageConstants.LOTTERY_TAKE_ALREADY);
            }
            //获取活动相关策略信息
            DrawReq drawReq = lotteryActivityService.getLotteryStrategyById(req.getActId());
            //进行抽奖操作
            if(drawReq==null){
                throw new BusinessException(ResultMessageConstants.ACT_END);
            }
            final DrawResult drawResult = drawService.doDrawExec(drawReq);
            if(drawResult==null){
                return Result.error(ResultMessageConstants.OPERATION_FAILED);
            }
            ActRedemptionVO resultVO = lotteryActivityService.createAwardRedemption(userId,req.getActId(),drawResult);
            return Result.ok().data(resultVO);
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        } catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultMessageConstants.OPERATION_FAILED);
        }
    }

    @PostMapping("/redemption")
    public Result redemption(@RequestBody ActRedemptionReq req){
        final Integer userId = UserHolder.get();
        if(ObjectUtils.isEmpty(userId)){
            return Result.error(ResultMessageConstants.NO_USER_TOKEN);
        }
        req.setUserId(userId);
        final RedemptionResult redemptionResult;
        try {
            redemptionResult = redemptionService.actRedemptionAward(req);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("兑换失败");
        }
        return Result.ok().data(redemptionResult);
    }

}
